using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[CreateAssetMenu(menuName = "SOInstance/Data/WeaponData/Bagpipe")]
public class BagpipeDataSO : WeaponDataSO
{
    public GameObject Oak3;

    protected override void OnEnable()
    {
        base.OnEnable();

        cd = new List<float>() { 1f, 3f, 3f, 4f, 4f, 5f };
        skillAreaData = new List<SkillAreaData>()
        {
            new (true, true,false, 15.0f,SkillMode.Area, SkillAreaType.Circle, 15, 1, true, false, 1),

            new (true, true,true, 0, SkillMode.Area, SkillAreaType.Circle, 8, 8, true, false, 1),
            new (true, true, true, 0, SkillMode.Area, SkillAreaType.Circle, 8, 8, true, false, 1),
            new (true, true,false, 0, SkillMode.Area, SkillAreaType.Circle, 1, 1, true, false, 1),
            new (true, true, true, 0, SkillMode.Area, SkillAreaType.Circle, 8, 8, true, false, 1),
            new (true, true, true, 0, SkillMode.Area, SkillAreaType.Circle, 25, 25, true, false, 1),
        };
    }

    public override IEnumerator Skill0(SkillData info)
    {
        Vector2 mousePos2D = info.MousePos;
        Player player = owner as Player;
        var hits = Util.GetObjsSortedByDistance(Util.FindNearbyTarget(mousePos2D, player ? player.controller.detectRange : 1, player.controller.AttackLayers), mousePos2D);
        var tar = hits.Select(obj => obj.GetComponent<Damageable>())
        .FirstOrDefault(c => c != null && c.teamType != owner.teamType);

        if (tar && tar.teamType != owner.teamType
            && Util.IsInDistance(tar.transform.position, owner.transform.position, skillAreaData[0].range))
        {
            info.DamageType = 0;
            info.Damage = owner.Stats[STATS.Attack].Value();

            var targets = Util.FindNearbyTarget(tar.transform.position, 1, player.controller.AttackLayers)
                .Select(go => go.GetComponent<Damageable>())
                .Where(enemy => enemy != null).ToList();

            foreach (var target in targets)
            {
                owner.ApplyDamage(target, info, true);
            }

            owner.StartSkillCD(0);
            if (player) player.controller.ClearPath();
            yield break;
        }
        yield return null;
        owner.RefreshSkill(0);
    }

    public override IEnumerator Skill1(SkillData info)
    {
        owner.StartSkillCD(1);
        yield break;
    }

    public override IEnumerator Skill2(SkillData info)
    {
        new Buff_Sleep(100f).Apply(owner, owner);
        owner.StartSkillCD(2);
        yield break;
    }

    public override IEnumerator Skill3(SkillData info)
    {
        owner.StartSkillCD(3);

        static void action(Buff b) { b.SetTimer(b.GetTimer() + 2f); }
        Buff.OnApply += action;
        yield return new WaitForSeconds(5f);
        Buff.OnApply -= action;

        yield break;
    }

    public override IEnumerator Skill4(SkillData info)
    {
        owner.StartSkillCD(4);
        yield break;
    }

    public override IEnumerator Skill5(SkillData info)
    {
        owner.StartSkillCD(5);
        yield break;
    }
}
